import requests
import json
from dotenv import load_dotenv
import os

# 加载.env文件
load_dotenv()

# sendUrl = "https://open.feishu.cn/open-apis/bot/v2/hook/548ff42c-9e02-4873-82de-3ca55bf99e82";


sendUrl = "https://open.feishu.cn/open-apis/bot/v2/hook/98a37198-1a4c-45b1-a462-569fc30038d6";

def getEnv(envName, type=1):
    if type == 0:
        return os.getenv(envName)
    if type == 1:
        return int(os.getenv(envName))


def sendInfoText(text):
    env = getEnv('ENV', 0)
    if env != 'dev':
        content = {
            "text": text
        }
        data = {
            "msg_type": "text",
            "content": content
        }

        headers = {"Content-Type": "application/json"}
        print(data)
        print(json.dumps(data))

        res = requests.post(sendUrl, data=json.dumps(data), headers=headers)
        print(res.text)
    else:
        print(text)


def sendInfo1(title, content):
    env = getEnv('ENV', 0)
    if env != 'dev':
        data = {
            "msg_type": "post",
            "content": {
                "post": {
                    "zh_cn": {
                        "title": title,
                        "content": [
                            [
                                {
                                    "tag": "text",
                                    "text": content
                                }
                            ]
                        ]
                    }
                }
            }
        }

        headers = {"Content-Type": "application/json"}
        requests.post(sendUrl, data=json.dumps(data), headers=headers)
    else:
        print(content)


def sendInfo(title, content, href):
    env = getEnv('ENV', 0)
    if env != 'dev':
        data = {
            "msg_type": "post",
            "content": {
                "post": {
                    "zh_cn": {
                        "title": title,
                        "content": [
                            [
                                {
                                    "tag": "text",
                                    "text": content
                                },
                                {
                                    "tag": "a",
                                    "text": "查看原文",
                                    "href": href
                                }
                            ]
                        ]
                    }
                }
            }
        }

        headers = {"Content-Type": "application/json"}
        requests.post(sendUrl, data=json.dumps(data), headers=headers)
    else:
        print(content)

